package com.dsg.service.apply.impl;

import com.dsg.common.util.UUIDGenerator;
import com.dsg.dao.apply.DsgApplyDdaSubMapper;
import com.dsg.entity.apply.DsgApplyDda;
import com.dsg.entity.apply.DsgApplyDdaSub;
import com.dsg.entity.apply.DsgApplyPay;
import com.dsg.entity.apply.DsgApplyStatus;
import com.dsg.entity.approve.DsgApproveCase;
import com.dsg.entity.system.DsgSysFile;
import com.dsg.service.DsgServiceImpl;
import com.dsg.service.apply.IDsgApplyDdaService;
import com.dsg.service.apply.IDsgApplyDdaSubService;
import com.dsg.service.apply.IDsgApplyPayService;
import com.dsg.service.approve.IDsgApproveCaseService;
import com.dsg.service.system.IDsgSysFileService;
import com.dsg.util.RRException;
import com.dsg.workflow.WorkflowEngine;
import com.dsg.workflow.WorkflowService3;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.OutputStream;
import java.util.Date;

/**
 * <p>
 * DDA設立記錄子表 服务实现类
 * </p>
 *
 * @author system
 * @since 2020-10-06
 */
@Service
public class DsgApplyDdaSubServiceImpl extends DsgServiceImpl<DsgApplyDdaSubMapper, DsgApplyDdaSub> implements IDsgApplyDdaSubService {

    @Autowired
    private IDsgApplyDdaService dsgApplyDdaService;
    @Autowired
    private IDsgApplyDdaSubService dsgApplyDdaSubService;
    @Autowired
    private IDsgApplyPayService dsgApplyPayService;
    @Autowired
    private IDsgSysFileService dsgSysFileService;
    @Autowired
    private IDsgApproveCaseService caseService;
    @Autowired
    private WorkflowEngine engine;
    @Autowired
    private IDsgSysFileService fileService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDdaStatus(DsgApplyDdaSub dsgApplyDdaSub) {
        updateById(dsgApplyDdaSub);
        dsgApplyDdaSub = getById(dsgApplyDdaSub.getOid());
        DsgApplyDda dsgApplyDda = dsgApplyDdaService.getById(dsgApplyDdaSub.getPid());
        dsgApplyDda.setDdaStatus(dsgApplyDdaSub.getDdaStatus());
        dsgApplyDdaService.updateById(dsgApplyDda);
        //回填
        DsgApplyPay dsgApplyPay = dsgApplyPayService.getById(dsgApplyDda.getPayOid());
        dsgApplyPay.setPayLimit(dsgApplyDdaSub.getPayLimit());
        dsgApplyPay.setPRDTY(dsgApplyDdaSub.getPrdty());
        dsgApplyPayService.updateById(dsgApplyPay);
        if (DsgApplyStatus.AppStatus.success.getCode().intValue() == dsgApplyDdaSub.getDdaStatus().intValue()) {
            DsgApproveCase approveCase = caseService.getCase(WorkflowService3.NODE_DDA, dsgApplyDdaSub.getAppNo());
            if (approveCase == null) {
                throw new RRException("對單CASE不存在");
            }
            try {
                if (DsgApproveCase.Status.started.getCode().intValue() == approveCase.getStatus().intValue()) {
                    engine.forward(approveCase);
                }
            } catch (Exception e) {
                log.error("推實例異常：", e);
                throw new RRException("推實例異常");
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void downloadFile(HttpServletResponse response, String id) throws Exception {
        DsgApplyDdaSub dsgApplyDdaSub = dsgApplyDdaSubService.getById(id);
//        AjaxResult ajaxResult = new AjaxResult(MessageConstants.SSO_STATUS_FAIL);
        if (dsgApplyDdaSub != null) {
            dsgApplyDdaSub.setDdaStatus(DsgApplyDdaSub.AppStatus.already.getCode());
            dsgApplyDdaSub.setDownloadTime(new Date());
            dsgApplyDdaSubService.updateById(dsgApplyDdaSub);
            DsgApplyDda dsgApplyDda = dsgApplyDdaService.getById(dsgApplyDdaSub.getPid());
            if (dsgApplyDda != null) {
                dsgApplyDda.setDdaStatus(DsgApplyDda.DDA_STATUS_RECORD);
                dsgApplyDdaService.updateById(dsgApplyDda);
            }
            DsgSysFile dsgSysFile = dsgSysFileService.getById(dsgApplyDdaSub.getRelId());
            if (dsgSysFile != null) {
                byte[] bytes = dsgSysFile.getFileContent();
                OutputStream os = response.getOutputStream();
                response.setContentType("application/octet-stream;charset=UTF-8");
                response.setHeader("Content-Length", "" + bytes.length);
                response.setHeader("Content-disposition", "attachment; filename=\"" +
                        (dsgApplyDdaSub.getAppNo()) + "." + dsgSysFile.getFileType() + "\"");
                os.write(bytes);
                os.flush();
                os.close();
//                ajaxResult.setCode(1);
//                ajaxResult.setData(dsgSysFile);
//                ajaxResult.setMessage("成功");
//                return ajaxResult;
            } else {
                throw new RRException("文件不存在！");
            }
        } else {
            throw new RRException("數據不存在！");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void importFile(File file, DsgApplyDdaSub dsgApplyDdaSub) throws Exception {
        DsgApplyDdaSub dads = getById(dsgApplyDdaSub.getOid());
        if (dads != null) {
            DsgApplyDdaSub ddaSub = new DsgApplyDdaSub();
            BeanUtils.copyProperties(dads, ddaSub);
            ddaSub.setOid(UUIDGenerator.getUUID());
            ddaSub.setPid(dads.getPid());
            ddaSub.setAppNo(dads.getAppNo());
            ddaSub.setAppType(dads.getAppType());
            ddaSub.setDdaStatus(DsgApplyDda.DDA_STATUS_NOT_RECORD);
            String fileType = file.getName().split("\\.")[1];
            DsgSysFile dsgSysFile = new DsgSysFile();
            fileService.saveFile(dsgSysFile, file, ddaSub.getOid(), fileType);
            ddaSub.setRelId(dsgSysFile.getOid());
            ddaSub.setCreateTime(null);
            ddaSub.setCreater(null);
            ddaSub.setUpdateTime(null);
            ddaSub.setUpdater(null);
            ddaSub.setDownloadTime(null);
            ddaSub.setUploadTime(null);
            save(ddaSub);
            DsgApplyDda dsgApplyDda = dsgApplyDdaService.getById(dads.getPid());
            dsgApplyDda.setNewNum(dsgApplyDda.getNewNum() + 1);
            dsgApplyDda.setDdaStatus(DsgApplyDda.DDA_STATUS_NOT_RECORD);
            dsgApplyDdaService.updateById(dsgApplyDda);
        }
    }
}
